//
//  LocalPlayer.h
//  CLocalPlayer: player kete players...
//
//  Created by Enric Vergara on 20.10.12. 
//  Copyright OWLAB 2010. All rights reserved.

#import "LocalPlayer.h"
#import "SoundManager.h"

@implementation CLocalPlayer


-(id) init
{
    if( (self=[super init])) 
    {
        m_eTypePlayer = PLAYER_LOCAL;
        
        //Cargo las imagenes ...
        CCLabel *label = [CCLabel labelWithString:NSLocalizedString(@"Iam_Local_Player", @"") fontName:@"Arial" fontSize:24];
        [label setColor:ccc3(0,0,0)];
        [label setPosition:CGPointMake(0, 90)];
        [self addChild: label];
        m_bOnEnterFirstTime = true;
        
        
        [CCMenuItemFont setFontName:@"Helvetica-BoldOblique"];
        [CCMenuItemFont setFontSize:26];
        
        button_Fold     = [CCMenuItemFont itemFromString:NSLocalizedString(@"Fold", @"") 
                                                  target:self selector:@selector(menuFoldTouched:)];
        button_Check    = [CCMenuItemFont itemFromString:NSLocalizedString(@"Check", @"") 
                                                  target:self selector:@selector(menuCheckTouched:)];
		button_Call     = [CCMenuItemFont itemFromString:NSLocalizedString(@"Call", @"") 
                                                  target:self selector:@selector(menuCallTouched:)];
		button_Raise    = [CCMenuItemFont itemFromString:NSLocalizedString(@"Raise", @"")
                                                  target:self selector:@selector(menuRaiseTouched:)];
		button_ALlIn    = [CCMenuItemFont itemFromString:NSLocalizedString(@"AllIn", @"") 
                                                  target:self selector:@selector(menuAllInTouched:)];
        
        button_Check.position   =  CGPointMake(0, 30);
		button_Call.position    =  CGPointMake(0, 60);
		button_Raise.position   =  CGPointMake(0, 90);
		button_ALlIn.position   =  CGPointMake(0, 120);
        
        // create the menu using the items
        menu = [CCMenu menuWithItems:button_Fold, button_Check, button_Call, button_Raise, button_ALlIn, nil];
        menu.position = CGPointMake(150, 0);
        [self addChild:menu];
        menu.visible = false;
        
        m_bFoldTouched  = false;
        m_bCheckTouched = false;
		m_bCheckTouched = false;
		m_bCallTouched  = false;
		m_bRaiseTouched = false;
		m_bAllInTouched = false;
        
        m_bOnEnterFirstTime = true;
        
        
        [CCMenuItemFont setFontName:@"Arial"];
        [CCMenuItemFont setFontSize:40];
        button_SeeCard  = [CCMenuItemFont itemFromString:NSLocalizedString(@"OpenCards", @"") 
                                                  target:self selector:@selector(menuSeeCards:)];
        menu2 = [CCMenu menuWithItems:button_SeeCard,nil];
        menu2.position = CGPointMake(-230, 0);
        [self addChild:menu2];
        menu2.visible = false;
        
        
        m_RaiseMenu = [CRaiseMenu node];
        [self addChild:m_RaiseMenu];
        m_RaiseMenu.position = CGPointMake(0, 100);
        m_RaiseMenu.visible = false;

    }
    return self;
}

- (void) notificationCallback:(NSNotification *) notification
{
    m_bRaiseTouched = true;
    m_uiRaise = [m_RaiseMenu getRaise];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void) menuFoldTouched: (id) sender
{
    [[SoundManager sharedInstance] playEffect:SOUND_BUTTON_SHOW_CARDS];
    m_bFoldTouched = true;
}

- (void) menuCheckTouched: (id) sender
{
    [[SoundManager sharedInstance] playEffect:SOUND_BUTTON_SHOW_CARDS];
    m_bCheckTouched = true;    
}

- (void) menuCallTouched: (id) sender
{
    [[SoundManager sharedInstance] playEffect:SOUND_BUTTON_SHOW_CARDS];
    m_bCallTouched = true;
}

- (void) menuRaiseTouched: (id) sender
{
    [[SoundManager sharedInstance] playEffect:SOUND_BUTTON_SHOW_CARDS];
    m_RaiseMenu.visible = true;
    [m_RaiseMenu setMaximumRaise:m_CheckOptions.m_uiRaiseAmmount];
    [[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(notificationCallback:)
												 name:nil
											   object:nil
	 ];
}

- (void) menuAllInTouched: (id) sender
{
    [[SoundManager sharedInstance] playEffect:SOUND_BUTTON_SHOW_CARDS];
    m_bAllInTouched = true;
}

- (void) menuSeeCards: (id) sender
{
    [[SoundManager sharedInstance] playEffect:SOUND_BUTTON_SHOW_CARDS];
    if (m_Sprite_CardLeft.visible)
    {
        [super hideCards];
    }
    else
    {
        [super showCards];
    }
}

- (void) resetStatus
{
    [super resetStatus];
    menu2.visible = false;
}
- (void) setHoleCards:(unsigned int)right LeftCard:(unsigned int) left
{
    [super setHoleCards:right LeftCard:left];
    menu2.visible = true;
}

- (bool) check:(unsigned int) currentBet deltaTime:(ccTime)delta firstBet:(bool) flag_firstBet;
{
    
    //------------------Primer de tot mirar si els jugadors son delear, smallBlend o bigBlend----
    if (flag_firstBet)
    {
        return [self checkFirstBet:delta];
    }
    //---------------------------------------------------------------------------------------------
    
    bool isEnd = false;
    if (m_bOnEnterFirstTime)
    {
        m_bOnEnterFirstTime = false;
        m_uiToBank = 0;
        m_uiCall = 0;
        m_uiRaise = 0;
        
		[self searchOptions:currentBet];
        [self setPlayerState:PlayerState_Thinking];
        
        
        [button_Fold setVisible:m_CheckOptions.m_bDoFold];
        [button_Call setVisible:m_CheckOptions.m_bDoCall];
        [button_Check setVisible:m_CheckOptions.m_bDoCheck];
        [button_Raise setVisible:m_CheckOptions.m_bDoRaise];
       
        menu.visible = true;
    }
    else
    {
		if (m_bFoldTouched)
        {
			m_bOnEnterFirstTime = true;
            m_bFoldTouched = false;
            isEnd = true;
            [self setPlayerState:PlayerState_Fold];
            menu.visible = false;
        }
        else if (m_bCheckTouched)
        {
            m_bOnEnterFirstTime = true;
            m_bCheckTouched = false;
            isEnd = true;
            [self setPlayerState:PlayerState_Check];
            menu.visible = false;
        }
		else if (m_bCallTouched)
		{
            m_uiCall = m_CheckOptions.m_uiCallAmmount;
			m_bOnEnterFirstTime = true;
            m_bCallTouched = false;
            isEnd = true;
            [self setPlayerState:PlayerState_Call];
            menu.visible = false;
		}
		else if (m_bRaiseTouched)
		{
            m_uiCall = m_CheckOptions.m_uiCallAmmount;
			m_bOnEnterFirstTime = true;
            m_bRaiseTouched = false;
            isEnd = true;
            [self setPlayerState:PlayerState_Raise];
            menu.visible = false;
		}
		else if (m_bAllInTouched)
		{
            m_uiCall = m_CheckOptions.m_uiCallAllIn;
            m_uiRaise = m_CheckOptions.m_uiRaiseAllIn;
			m_bOnEnterFirstTime = true;
            m_bAllInTouched = false;
            isEnd = true;
            [self setPlayerState:PlayerState_AllIn];
            menu.visible = false;
		}
    }
    
    return isEnd;
}

- (void) dealloc
{
	// don't forget to call "super dealloc"
	[super dealloc];
}
@end